package com.qzdsoft.erpcloud.controller.stock;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.pu.PurchaseOrder;
import com.qzdsoft.erpcloud.domain.stock.StockInbound;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseOrderQueryMapper;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.pu.PurchaseOrderService;
import com.qzdsoft.erpcloud.service.stock.StockInboundService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundQueryInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.ReviewStatus;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.pu.InboundType;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;
import com.qzdsoft.vo.pu.StockInboundOrderStatus;
/**
 * 采购入库单
 * @author pc-20170422
 *
 */
@Controller
@RequestMapping("/stock/inbound")
public class StockInboundController extends BaseController {
    
    private static final String BASEPATH = "stock/inbound";
    private static final Logger logger = LoggerFactory.getLogger(StockInboundController.class);
    
    @Autowired
    private StockInboundService stockInboundService;
    @Autowired
    private CustomerSupplierService customerSupplierService;
    @Autowired
    private PurchaseOrderService purchaseOrderService;
    
    @Autowired
    private CustomerSupplierStatementService customerSupplierStatementService;
    /**
     * 采购入库单首页
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/index");
        return mv;
    }
    /**
     * 采购入库单条件查询
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<StockInboundInfo> list(StockInboundQueryInfo info){
    	info.setInboundType(Integer.parseInt(InboundType.BUY_CODE));
    	logger.debug("入库类型:type={}",info.getInboundType());
    	//如果初始化查询返回无数据空页
        if(info.isFirst()) {
            return LayTableResponse.nullDataPage();
        }else {
        	return stockInboundService.findStockInboundOrdersBy(info);
        }
    }
    /**
     * 打开编辑页面
     * @param companyid
     * @param id
     * @return
     */
//    @RequestMapping("/{companyid}/{id}")
//    public ModelAndView toedit(@PathVariable("companyid")String companyid,@PathVariable("id")Integer id) {
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView toedit(Integer id) {
        logger.debug("查询采购入库单详情：id={}",id);
        ModelAndView mv = new ModelAndView();
        StockInboundInfo order = stockInboundService.findStockInboundOrderBy(id);
        mv.addObject("order", order);
        logger.debug("采购入库单编辑:id={},suplierId={},supelierContactId={}",order.getId(),order.getSupplyId(),order.getSupplyContactId());
        logger.debug("采购入库单编辑2:StatementName={},StatementId={},StatementTaxRate={},StatementDays={},StatementCurrencyId={}",
        		order.getStatementName(),order.getStatementId(),order.getTaxRate(),
        		order.getStatementDays(),order.getCurrencyId());
//        List<TypeInfo> status = PurchaseOrderStatus.ALL;
//        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/edit");
        return mv;
    }
    /**
     * 采购入库单修改保存
     * @param info
     * @return
     */
    @SysLog(description="采购入库单修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Response<String> edit(StockInbound info) {
        logger.info(info.getId()+","+info.getRemark());
        logger.debug("采购入库单编辑:id={},suplierId={},supelierContactId={}",info.getId(),info.getSupplyId(),info.getSupplyContactId());
        logger.debug("采购入库单编辑2:StatementName={},StatementId={},StatementTaxRate={},StatementDays={},StatementCurrencyId={}",
    			info.getStatementName(),info.getStatementId(),info.getStatementTaxRate(),
    			info.getStatementDays(),info.getStatementCurrencyId());
        return stockInboundService.edit(info);
    }
    /**
     * 打开新增页面
     * @param companyid
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView toadd(String companyId) {
    	logger.debug("采购入库单新增：companyid={}",companyId);
    	logger.debug(companyId);
    	//查询来源单据号
    	PurchaseOrder purInfo = new PurchaseOrder();
    	purInfo.setCompanyId(Integer.parseInt(companyId));
    	purInfo.setStatus(Integer.parseInt(StockInboundOrderStatus.HAS_REVIEW_CODE));
    	List<PurchaseOrder> purchaseOrders = purchaseOrderService.selectReviewOrderByCompanyId(purInfo);
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("purchaseOrders", purchaseOrders);
    	mv.setViewName(BASEPATH+"/add");
    	return mv;
    }
    
    /**
     * 查询来源入库单号信息
     * @param id
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/purchaseOrder/{id}")
    public Response getpurchaseOrderById(@PathVariable("id")Integer id) {
    	//查询来源单据号
    	logger.debug("查询来源单据号:sourceid={}",id);
    	return stockInboundService.selectBySourceId(id);
    }
    
    /**
     * 供应商税率信息查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/queryTax/{supplierid}/{companyid}")
    public TypeInfo queryTax(@PathVariable("supplierid")Integer supplierid,@PathVariable("companyid")Integer companyid) {
        return customerSupplierService.selectTaxInfo(companyid, supplierid);
        
    }
    
    /**
     * 供应商结算方式查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/querySettleMethod/{supplierid}")
    public List<CustomerSupplierStatementInfo> querySettleMethod(@PathVariable("supplierid")Integer supplierid) {
        //供应商结算方式
       LoginUser user = getUser();
       Integer companyid = user.getCompanyId();
       List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService.selectBy(companyid, supplierid);
       return customerSupplierStatements;
    }
    
    /**
     * 新增表头保存
     * @param info
     * @return
     */
    @SysLog(description="采购入库单新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Response<String> addSave(StockInbound info) {
    	logger.debug("采购入库单新增保存:SourceBillId={},BillNo={},SupplyId={},SupplyContactId={},sourceBillno={}",
    			info.getSourceBillId(),info.getBillNo(),info.getSupplyId(),info.getSupplyContactId(),info.getSourceBillNo());
      	logger.debug("采购入库单新增保存:StatementName={},StatementId={},StatementTaxRate={},StatementDays={},StatementCurrencyId={}",
    			info.getStatementName(),info.getStatementId(),info.getStatementTaxRate(),
    			info.getStatementDays(),info.getStatementCurrencyId());
    	//设置入库类型
    	info.setInboundType(Integer.parseInt(InboundType.BUY_CODE));
    	return stockInboundService.save(info);
    }
    
    /**
     * 采购入库删除
     * @param info
     * @return
     */
    @SysLog(description="采购入库单删除")
    @ResponseBody
    @RequestMapping("/del")
    public Response<String> del(Integer id) {
//    	logger.info(info.getBillNo()+":"+info.getRemark());
    	logger.debug("采购入库单删除:id={}",id);
    	return stockInboundService.del(id);
    }
   /**
    * 采购入库单据查看
    * @param id
    * @return
    */
    @Permission(validate=false)
    @RequestMapping("/view/{id}")
    public ModelAndView view(@PathVariable("id")Integer id) {
    	logger.debug("采购入库单据查看:id={}",id);
    	ModelAndView mv = new ModelAndView();
    	StockInboundInfo order = stockInboundService.findStockInboundOrderBy(id);
        mv.addObject("order", order);
        mv.setViewName(BASEPATH+"/view");
    	return mv;
    }
    /**
     * 采购入库单审核
     * @param id
     * @return
     */
    @SysLog(description="采购入库单审核")
    @ResponseBody
    @RequestMapping("/review")
    public Response<String> review(Integer id) {
    	logger.debug("采购入库单据弃审:id={}",id);
    	return stockInboundService.review(id);
    }
    
    
    /**
     * 采购入库单弃审
     * @param id
     * @return
     */
    @SysLog(description="采购入库单弃审")
    @ResponseBody
    @RequestMapping("/celview")
    public Response<String> celview(Integer id) {
    	logger.debug("采购入库单据弃审:id={}",id);
    	return stockInboundService.celview(id);
    }
    
    /**
     * 采购入库单关闭
     * @param id
     * @return
     */
    @SysLog(description="采购入库单关闭")
    @ResponseBody
    @RequestMapping("/close")
    public Response<String> close(Integer id) {
    	logger.debug("采购入库单据关闭:id={}",id);
    	return stockInboundService.close(id);
    }
}
